package imotech.ejb.api;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.MessageFactory;

@Stateless
public class SmsAPIBean implements SmsAPI {
	public static final String ACCOUNT_SID = "ACc2770c21ec09d98bde0031ac13b24e1f";
	public static final String AUTH_TOKEN = "1149b89827f990dfc1183371a56cc8bf";
	public static final String FROM = "18654844729";
	  
	private TwilioRestClient client;
	
	public SmsAPIBean() {
		this.client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
	}
	
	public void send(String to, String message) throws TwilioRestException {
		getMessageFactory().create(getMessageParams(to, message));
	}
	
	private List<NameValuePair> getMessageParams(String to, String message) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
	    params.add(new BasicNameValuePair("Body", message));
	    params.add(new BasicNameValuePair("From", FROM));
	    params.add(new BasicNameValuePair("To", "+55" + to));
	    return params;
	}
	
	private MessageFactory getMessageFactory() {
		return client.getAccount().getMessageFactory();
	}
}
